package com.sky.controller.admin;

import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 管理端订单控制器
 */
@RestController("adminOrderController")
@RequestMapping("/admin/order")
@Api(tags = "管理端订单相关接口")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 订单分页查询
     * @param ordersPageQueryDTO 查询条件
     * @return 分页结果
     */
    @GetMapping("/page")
    @ApiOperation("订单分页查询")
    public Result<PageResult> pageQuery(@RequestBody OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("订单分页查询：{}", ordersPageQueryDTO);
        PageResult pageResult = orderService.pageQueryAdmin(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 订单详情查询
     * @param id 订单ID
     * @return 订单详情
     */
    @GetMapping("/{id}")
    @ApiOperation("查询订单详情")
    public Result<OrderVO> getOrderDetail(@PathVariable Long id) {
        log.info("查询订单详情：id = {}", id);
        OrderVO orderVO = orderService.getOrderDetail(id);
        return Result.success(orderVO);
    }

    /**
     * 接单
     * @param ordersConfirmDTO 确认接单数据
     * @return 操作结果
     */
    @PutMapping("/confirm")
    @ApiOperation("接单")
    public Result confirmOrder(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {
        log.info("接单：{}", ordersConfirmDTO);
        orderService.confirmOrder(ordersConfirmDTO);
        return Result.success();
    }

    /**
     * 商家取消订单
     * @param id 订单ID
     * @return 操作结果
     */
    @PutMapping("/cancel")
    @ApiOperation("商家取消订单")
    public Result cancelOrder(@RequestParam Long id) {
        log.info("商家取消订单：id = {}", id);
        orderService.adminCancelById(id);
        return Result.success();
    }

    /**
     * 开始派送
     * @param id 订单ID
     * @return 操作结果
     */
    @PutMapping("/delivery/{id}")
    @ApiOperation("开始派送")
    public Result delivery(@PathVariable Long id) {
        log.info("开始派送：id = {}", id);
        orderService.startDelivery(id);
        return Result.success();
    }

    /**
     * 完成订单
     * @param id 订单ID
     * @return 操作结果
     */
    @PutMapping("/complete/{id}")
    @ApiOperation("完成订单")
    public Result complete(@PathVariable Long id) {
        log.info("完成订单：id = {}", id);
        orderService.completeDelivery(id);
        return Result.success();
    }

    /**
     * 获取订单统计数据
     * @return 订单统计信息
     */
    @GetMapping("/statistics")
    @ApiOperation("获取订单统计数据")
    public Result<com.sky.vo.OrderStatisticsVO> statistics() {
        log.info("获取订单统计数据");
        com.sky.vo.OrderStatisticsVO statistics = orderService.getStatistics();
        return Result.success(statistics);
    }

    /**
     * 拒单
     * @param id 订单ID
     * @param reason 拒单原因
     * @return 操作结果
     */
    @PutMapping("/rejection/{id}")
    @ApiOperation("拒单")
    public Result rejection(@PathVariable Long id, @RequestParam String reason) {
        log.info("拒单：id = {}, reason = {}", id, reason);
        // TODO: 实现拒单功能
        return Result.success();
    }

    /**
     * 客户催单
     * @param id 订单ID
     * @return 操作结果
     */
    @PutMapping("/reminder/{id}")
    @ApiOperation("客户催单")
    public Result reminder(@PathVariable Long id) {
        log.info("客户催单：id = {}", id);
        // TODO: 实现客户催单功能
        return Result.success();
    }
}